package com.inifitness.common.xxl;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * XXL-JOB服务配置
 *
 * @author sunjinfeng
 * @version 1.0.0
 * @since 2024/04/18 11:00:00
 */
@Configuration
@Log4j2
@RequiredArgsConstructor
@ConditionalOnProperty(name = "xxl.job.enabled")
@EnableConfigurationProperties(XxlJobProperties.class)
public class XxlJobConfig {

  private final XxlJobProperties xxlJobProperties;

  @Bean
  public XxlJobSpringExecutor xxlJobExecutor() {
    log.info("===> XXL-JOB Bean执行开始");
    XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
    xxlJobSpringExecutor.setAdminAddresses(this.xxlJobProperties.getAdmin().getAddresses());
    xxlJobSpringExecutor.setAppname(this.xxlJobProperties.getExecutor().getAppname());
    xxlJobSpringExecutor.setAddress(this.xxlJobProperties.getExecutor().getAddress());
    xxlJobSpringExecutor.setIp(this.xxlJobProperties.getExecutor().getIp());
    xxlJobSpringExecutor.setPort(this.xxlJobProperties.getExecutor().getPort());
    xxlJobSpringExecutor.setAccessToken(this.xxlJobProperties.getAccessToken());
    xxlJobSpringExecutor.setLogPath(this.xxlJobProperties.getExecutor().getLogPath());
    xxlJobSpringExecutor.setLogRetentionDays(
        this.xxlJobProperties.getExecutor().getLogRetentionDays());
    log.info("<=== XXL-JOB Bean执行成功");
    return xxlJobSpringExecutor;
  }
}
